#include <iostream>
#include <BinaryTree.hpp>

using namespace std;

class Solution
{
    int lastVal = 0;
    bool first = true;

public:
    bool isValidBST(TreeNode *root)
    {
        return dfs(root);
    }

    bool dfs(TreeNode *root)
    {
        if (root->left == nullptr && root->right == nullptr)
        {
            cout << root->val << ',';
            if (first)
            {
                first = false;
                lastVal = root->val;
                return true;
            }
            if (lastVal >= root->val)
            {
                return false;
            }
            lastVal = root->val;
            return true;
        }
        if (root->left != nullptr)
        {
            if (!dfs(root->left))
                return false;
        }
        cout << root->val << ',';
        if (first)
        {
            first = false;
            lastVal = root->val;
        }
        else
        {
            if (lastVal >= root->val)
            {
                return false;
            }
            lastVal = root->val;
        }
        if (root->right != nullptr)
        {
            if (!dfs(root->right))
                return false;
        }
        return true;
    }
};

int main()
{
    cout << "Code98" << endl;
    Solution s;
    TreeNode *root = CreatTree("[0,null,1]", "null");
    cout << boolalpha << s.isValidBST(root);
    return 0;
}